<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic;

use OSS\OssClient;
use OSS\Core\OssException;
use think\Db;

class YiDunLogic
{
    public $yidun;
    public $secretId;
    public $version;
    public $secretKey;
    public $common_param;

    public function __construct()
    {
        $yidunConfig = sysConfig('yidun');
        $this->yidun = $yidunConfig;
        $this->secretId = isset($yidunConfig['secretId']) ? $yidunConfig['secretId'] : '';
        $this->secretKey = isset($yidunConfig['secretKey']) ? $yidunConfig['secretKey'] : '';
        $this->version = isset($yidunConfig['version']) ? $yidunConfig['version'] : '';

        //设置公共参数
        $param['secretId'] = $this->secretId;
        $param['secretKey'] = $this->secretKey;
        $param['version'] = $this->version;

        $this->common_param = $param;

    }


    /**
     * User: Simony
     * Function:活体检测后端接口
     * @param $token SDK上传给业务服务端的token，唯一标记一次活体检测，有效期20分钟
     * @param $needAvatar 需要返回正面照传true；不需要返回正面照传false  响应结果
     */
    public function liveperson($token, $needAvatar = "true")
    {
        //公共参数
        $param = $this->common_param;
        $param['timestamp'] = getMillisecond();
        $param['nonce'] = mt_rand(10000, 99999) . $param['timestamp'];

        //私有参数
        $param['businessId'] = isset($this->yidun['living']['businessId']) ? $this->yidun['living']['businessId'] : '';
        $param['token'] = $token;
        $param['needAvatar'] = $needAvatar;
        $param['signature'] = $this->getSign($param);


        $url = "https://verify.dun.163.com/v1/liveperson/recheck";
        $data = http_request($url, 'POST', $param);
        $data = json_decode($data, true);
        return ds_arr($data['code'], $data['msg'], isset($data['result']) ? $data['result'] : []);
    }

    /**
     * User: Simony
     * Function:人脸比对接口
     * @param $avatar1 用于人脸比对的头像照，请上传无遮挡的清晰正面照，轮廓保持完整
     * @param $avatar2 用于人脸比对的头像照，请上传无遮挡的清晰正面照，轮廓保持完整
     * @param $picType 图片类型：1-URL, 2-BASE64
     */
    public function facecompare($avatar1, $avatar2, $picType = 1)
    {
        //公共参数
        $param = $this->common_param;
        $param['timestamp'] = getMillisecond();
        $param['nonce'] = mt_rand(10000, 99999) . $param['timestamp'];
        //私有参数
        $param['businessId'] = isset($this->yidun['face']['businessId']) ? $this->yidun['face']['businessId'] : '';
        $param['avatar1'] = $avatar1;
        $param['avatar2'] = $avatar2;
        $param['picType'] = $picType;

        $param['signature'] = $this->getSign($param);
        $url = "https://verify.dun.163.com/v1/facecompare/check";
        $data = http_request($url, 'POST', $param);

        $data = json_decode($data, true);

        return ds_arr($data['code'], $data['msg'], isset($data['result']) ? $data['result'] : []);
    }


    /**
     * User: Simony
     * Function:获取参数签名
     * @param  $params  要传递的参数数组
     * @return String
     */
    private function getSign($params)
    {
        ksort($params);        //将参数数组按照参数名ASCII码从小到大排序
        $string = '';
        foreach ($params as $key => $item) {
            if (!is_null($item)) $string .= $key . $item;
        }
        $string .= $this->secretKey;
        $sign = md5(mb_convert_encoding($string, "utf8", "auto"));
        return $sign;
    }
}